//
//  ViewController.m
//  02_作业
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 zuzhiming. All rights reserved.
//

#import "ViewController.h"
#import "CustomLabel.h"
#import "CustomTextField.h"
#import "CustomButton.h"
@interface ViewController ()<UITextFieldDelegate>
@property (nonatomic, retain)CustomTextField *customTextField;

@end

@implementation ViewController

- (void)dealloc
{
    [_customTextField release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //加载自定义Label
    [self createCustomLabel];
    
    //加载自定义TextField
    [self createCusomTextField];
    
    //加载button
    [self createCustomButton];
    
    //错误位置 应该写在循环里边 对象初始化之后
//    _customTextField.delegate = self;
    
}

//初始化自定义Label
- (void)createCustomLabel
{
    NSArray *array = @[@"用户名", @"密码", @"确认密码", @"手机号", @"邮箱"];
    for (NSInteger i = 0; i < 5; i++) {
        CustomLabel *customLabel = [[CustomLabel alloc] initWithFrame:CGRectMake(50, i * 50 + 50, 80, 30) WithText:[array objectAtIndex:i] WithFont:[UIFont systemFontOfSize:17.0f]];
        [self.view addSubview:customLabel];
        [customLabel release];
    }
}

//初始化自定义textField
- (void)createCusomTextField
{
    NSArray *array = @[@"请输入用户名", @"请输入密码", @"再次输入密码", @"请输入邮箱", @"请输入联系方式"];
    for (NSInteger i = 0; i < 5; i++) {
        self.customTextField = [[CustomTextField alloc] initWithFrame:CGRectMake(150, i * 50 + 50, 175, 30) WithPlaceholder:[array objectAtIndex:i]];
        
        _customTextField.delegate = self;
        if (i == 1 || i == 2) {
            //密文输入
            self.customTextField.secureTextEntry = YES;
        }
        if (i == 3) {
            //数字键盘
            self.customTextField.keyboardType = UIKeyboardTypeNumberPad;
        }
        [self.view addSubview:_customTextField];
        [_customTextField release];
    }
}

//初始化自定义button
- (void)createCustomButton
{
    NSArray *array = @[@"注册", @"取消"];
    for (NSInteger i = 0; i < 2; i++) {
        CustomButton *customButton = [CustomButton buttonWithType:UIButtonTypeSystem WithFrame:CGRectMake(100 + i * 150, 350, 50, 20) WithTitle:array[i]];
        [self.view addSubview:customButton];
        [customButton release];
    }
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//    textField = _customTextField;
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
